
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





Python 代理[#](#python-agent "永久链接到此标题")
======================================

本教程展示了一个代理程序，旨在编写和执行Python代码来回答问题。


```python
from langchain.agents.agent_toolkits import create_python_agent
from langchain.tools.python.tool import PythonREPLTool
from langchain.python import PythonREPL
from langchain.llms.openai import OpenAI

```



```python
agent_executor = create_python_agent(
    llm=OpenAI(temperature=0, max_tokens=1000),
    tool=PythonREPLTool(),
    verbose=True
)

```


斐波那契例子[#](#fibonacci-example "永久链接到此标题")
----------------------------------------

这个例子是由[John Wiseman](https://twitter.com/lemonodor/status/1628270074074398720?s=20)创建的。


```python
agent_executor.run("What is the 10th fibonacci number?")

```



```python
> Entering new AgentExecutor chain...
 I need to calculate the 10th fibonacci number
Action: Python REPL
Action Input: def fibonacci(n):
 if n == 0:
 return 0
 elif n == 1:
 return 1
 else:
 return fibonacci(n-1) + fibonacci(n-2)
Observation: 
Thought: I need to call the function with 10 as the argument
Action: Python REPL
Action Input: fibonacci(10)
Observation: 
Thought: I now know the final answer
Final Answer: 55

> Finished chain.

```



```python
'55'

```


训练神经网络[#](#training-neural-net "永久链接到此标题")
------------------------------------------

这个例子是由[Samee Ur Rehman](https://twitter.com/sameeurehman/status/1630130518133207046?s=20)创建的。


```python
agent_executor.run("""Understand, write a single neuron neural network in PyTorch.
Take synthetic data for y=2x. Train for 1000 epochs and print every 100 epochs.
Return prediction for x = 5""")

```



```python
> Entering new AgentExecutor chain...
 I need to write a neural network in PyTorch and train it on the given data.
Action: Python REPL
Action Input: 
import torch

# Define the model
model = torch.nn.Sequential(
 torch.nn.Linear(1, 1)
)

# Define the loss
loss_fn = torch.nn.MSELoss()

# Define the optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# Define the data
x_data = torch.tensor([[1.0], [2.0], [3.0], [4.0]])
y_data = torch.tensor([[2.0], [4.0], [6.0], [8.0]])

# Train the model
for epoch in range(1000):
 # Forward pass
 y_pred = model(x_data)

 # Compute and print loss
 loss = loss_fn(y_pred, y_data)
 if (epoch+1) % 100 == 0:
 print(f'Epoch {epoch+1}: loss = {loss.item():.4f}')

 # Zero the gradients
 optimizer.zero_grad()

 # Backward pass
 loss.backward()

 # Update the weights
 optimizer.step()

Observation: Epoch 100: loss = 0.0013
Epoch 200: loss = 0.0007
Epoch 300: loss = 0.0004
Epoch 400: loss = 0.0002
Epoch 500: loss = 0.0001
Epoch 600: loss = 0.0001
Epoch 700: loss = 0.0000
Epoch 800: loss = 0.0000
Epoch 900: loss = 0.0000
Epoch 1000: loss = 0.0000

Thought: I now know the final answer
Final Answer: The prediction for x = 5 is 10.0.

> Finished chain.

```



```python
'The prediction for x = 5 is 10.0.'

```


